import java.text.ParseException;
import java.sql.*;
import java.util.Calendar;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;


public class GeoFun{
String time ;
/*public static void main (String[] args) {

}*/

/*This method converts the date into a string format and returns the
 * string value after conversion.
 */
  public String convertDateToString(){
     DateFormat dateFormat1 = new SimpleDateFormat("HH:mm:ss");
     Calendar calendar = Calendar.getInstance();
        int hour =calendar.get(calendar.HOUR_OF_DAY);
        int minut = calendar.get(calendar.MINUTE);
        int sec =  calendar.get(calendar.SECOND);
   time =  hour+ ":" +minut;
  
 //Code to add 5 Hours and 30 minutes to the current Time
  // calendar.add(Calendar.HOUR, 5);
 // calendar.add(Calendar.MINUTE, 30);
 // System.out.println(calendar.getTime());
  System.out.println("Time is :"+ time);
        return (time);
    }
    public Connection getConnection() throws Exception {
      Runtime.getRuntime().freeMemory();
     String driver = "com.mysql.jdbc.Driver";
     String url = "jdbc:mysql://localhost/chekhra_tracker_main";
     String username = "chekhra29997";
     String password = "chekhra29997";
     Class.forName(driver);
     Connection conn = DriverManager.getConnection(url, username, password);
     return conn;
 }
 public static String FilterIt(String source) {
     int i, len = source.length();
     StringBuffer dest = new StringBuffer(len);
     int tmp;
     for (i = 0; i< (len - 1); i++)
     {
       tmp = source.charAt(i);
       if((tmp >= 48 && tmp <= 57) || (tmp >= 65 && tmp <= 90) || (tmp >= 97 && tmp <= 122)) // || tmp == 124 || tmp == 44 || tmp == 36 || tmp == 42 || tmp == 46)
       {
         //System.out.println("int "+tmp+" "+source.charAt(i));         
         dest.append(source.charAt(i));
       }
     }
     return dest.toString();
 }
 public static String reverseIt(String source) {
     int i, len = source.length();
     StringBuffer dest = new StringBuffer(len);
     for (i = (len - 1); i >= 0; i--)
      dest.append(source.charAt(i));
     return dest.toString();
 }
 public static double round(double value, int decimalPlace)
    {
      double power_of_ten = 1;
      // floating point arithmetic can be very tricky.
      // that's why I introduce a "fudge factor"
      double fudge_factor = 0.05;
      while (decimalPlace-- > 0) {
         power_of_ten *= 10.0d;
         fudge_factor /= 10.0d;
      }
      return Math.round((value + fudge_factor)* power_of_ten)  / power_of_ten;
    }

 public String makeDate(String d1,String m1,String y1,String h1,String n1,String s1)
 {
     int d2=Integer.parseInt(d1);
     int m2=Integer.parseInt(m1);
     int y2=2000+Integer.parseInt(y1);
     int h2=Integer.parseInt(h1);
     int n2=Integer.parseInt(n1);
     int s2=Integer.parseInt(s1);
     
     SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
     SimpleDateFormat timeFormat = new SimpleDateFormat("dd-MM-yyyy");
     String strDate=null;
        
        String in = d2 + "-" + m2 + "-" + y2+" "+(h2+5)+":"+(n2+30)+":"+s2;
       
        try 
        {            
           Date theDate = dateFormat.parse(in);
           strDate= timeFormat.format(theDate).toString();
            
        } catch (ParseException e) {
            //e.printStackTrace();
        }
        return strDate;     
 }
 public static String secondsToString(long time){
  int seconds = (int)(time % 60);
  int minutes = (int)((time/60) % 60);
  int hours = (int)((time/3600) % 24);
  String secondsStr = (seconds<10 ? "0" : "")+ seconds;
  String minutesStr = (minutes<10 ? "0" : "")+ minutes;
  String hoursStr = (hours<10 ? "0" : "")+ hours;
  return new String(hoursStr + ":" + minutesStr + ":" + secondsStr);
  }
 public String HexToBinary(String userInput)
{
 String[]hex={"0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"};
 String[]binary={"0000","0001","0010","0011","0100","0101","0110","0111","1000","1001","1010","1011","1100","1101","1110","1111"};


 String result="";
 for(int i=0;i<userInput.length();i++)
 {
  char temp=userInput.charAt(i);
  String temp2=""+temp+"";
  for(int j=0;j<hex.length;j++)
  {
   if(temp2.equalsIgnoreCase(hex[j]))
   {
    result=result+binary[j];
   }
  }
 }
 return result;
}
public String convertStringToHex(String str){
 
   char[] chars = str.toCharArray();
 
   StringBuffer hex = new StringBuffer();
   for(int i = 0; i < chars.length; i++){
     hex.append(Integer.toHexString((int)chars[i]));
   }
 
   return hex.toString();
  }
 public String makeDateTime(String d1,String m1,String y1,String h1,String n1,String s1)
 {
     int d2=Integer.parseInt(d1);
     int m2=Integer.parseInt(m1);
     int y2=2000+Integer.parseInt(y1);
     int h2=Integer.parseInt(h1);
     int n2=Integer.parseInt(n1);
     int s2=Integer.parseInt(s1);
     
     SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
     SimpleDateFormat timeFormat = new SimpleDateFormat("dd-MM-yyyy,HH:mm:ss");
     String strDate=null;
        
        String in = d2 + "-" + m2 + "-" + y2+" "+(h2+5)+":"+(n2+30)+":"+s2;
       
        try 
        {            
           Date theDate = dateFormat.parse(in);
           strDate = timeFormat.format(theDate).toString();
            
        } catch (ParseException e) {
            //e.printStackTrace();
        }
        return strDate;     
 }
 public String calLat(String lat1) {
  String latArr= "";
  StringBuffer lat = new StringBuffer(reverseIt(lat1));
  int i, len=lat1.length();
  int l2=0;
  for (i = 0; i < len; i++) {
   if(i==7) {
    latArr +=" "+lat.charAt(i);
    l2++;
   }else if(i==5) {
    latArr+="."+lat.charAt(i);
    l2++;
   }else if(i==1){
    latArr+=" "+lat.charAt(i);
    l2++;
    }
   else{
    latArr+=lat.charAt(i);
    l2++;
   }
  }
  latArr=(reverseIt(latArr));
  return latArr;
   }
   public String calLong(String lng1) {
  String longArr= "";
  StringBuffer lng = new StringBuffer(reverseIt(lng1));
  int i, len=lng1.length();
  int l2=0;
  for (i = 0; i < len; i++){
   if(i==7){
    longArr +=" "+lng.charAt(i);
    l2++;
    }else if(i==5){
    longArr+="."+lng.charAt(i);
    l2++;
    }else if(i==1){
    longArr+=" "+lng.charAt(i);
    l2++;
     }else{
    longArr+=lng.charAt(i);
    l2++;
       }
   }
  longArr=(reverseIt(longArr));
  return longArr;
  }
  public String arrayToString(String[] stringarray){
     String str = " ";
     for (int i = 0; i < stringarray.length; i++) {
        str = str + stringarray[i];
     }
     return str;
  }
  public String convert(String v) {
  String[] D = v.split(" ");
  String[] M1 = D[1].split("\\."); 
  double s = ((Float.parseFloat(M1[1])/10000)*60);  
  String res=D[0]+" "+M1[0]+" "+s+" "+D[2];
  return (res);
  }
  public double convertLat(String lat) {
  String[] lat1=convert(lat).split(" ");
  double lat3=0.0;
     double LatDeg = Float.parseFloat(lat1[0]);
     double LatMin = Float.parseFloat(lat1[1]);
     double LatSec = Float.parseFloat(lat1[2]);
  if (LatDeg==0){
       LatDeg=0;
     }
     if (LatMin==0){
       LatMin=0;
     }
     if (LatSec==0){
       LatSec=0;
     } 
     if (LatDeg != Math.round(LatDeg) || LatMin != Math.round(LatMin) ){
        System.out.println("ERROR");
     }else if (LatDeg < -90 || LatDeg > 90 || LatMin < -60 || LatMin > 60 || LatSec < -60 || LatSec > 60 ){
          System.out.println("ERROR");
     }else{
      String LatDMSDirect = lat1[3];
      if (LatDMSDirect==null){
         if (LatDeg<0) {
           LatDMSDirect = "S";
          }else {
            LatDMSDirect ="N";
           }
      }
   LatDeg = Math.abs(LatDeg);
      LatMin = Math.abs(LatMin);
      LatSec = Math.abs(LatSec);
      lat3 = LatDeg + (LatMin/60) + (LatSec / 60 / 60);
      if ( lat3 <= 90 && lat3 >=0 ) {
         lat3 = lat3;
       } 
     }
     return lat3;
  }
  public double convertLong(String lng) {
  String[] lng1=convert(lng).split(" ");
  double lon3=0.0;

     // Retrieve Lat and Lon information
     double LonDeg = Float.parseFloat(lng1[0]);
     double LonMin = Float.parseFloat(lng1[1]);
     double LonSec = Float.parseFloat(lng1[2]);
 
     // Assume the value to be zero if the user does not enter value
     if (LonDeg==0)
       LonDeg=0;
     if (LonMin==0) {
       LonMin=0;
     }
     if (LonSec==0){
         LonSec=0;
     } 
     // Check if any error occurred
     if (LonDeg != Math.round(LonDeg) || LonMin != Math.round(LonMin)) {
        System.out.println("ERROR");
     }else if (LonDeg < -180 || LonDeg > 180 || LonMin < -60 || LonMin > 60 || LonSec < -60 || LonSec > 60) {
          System.out.println("ERROR");
       }else {
         // If no error, then go on
       // Retrieve the longitude direction for Deg/Min/Sec
          String LonDMSDirect = lng1[3];
      
       // If the user does not click direct button,
       // then a positive latitude value regards East, negative latitude value regards West
    if (LonDMSDirect==null) {
         if (LonDeg<0) {
            LonDMSDirect = "W";
                
       }else {
             LonDMSDirect ="E";
           }
        }
          // Change to absolute value
    LonDeg = Math.abs(LonDeg);
    LonMin = Math.abs(LonMin);
    LonSec = Math.abs(LonSec);
   
    // Convert to Decimal Degrees Representation
     lon3 = LonDeg + (LonMin/60) + (LonSec / 60 / 60);
        if ( lon3 <= 180 && lon3 >= 0 ){
           // Rounding off
           lon3 = lon3;
       } 
    }return lon3;
     }
     
      /*getDay() Method returns day of the Week in Integer format as Sunday-0,Monday-1 etc..,*/
 public int getDay() {
  Calendar cals =  Calendar.getInstance();
     int dayOfWeek = cals.get(Calendar.DAY_OF_WEEK);    
    return dayOfWeek-1;
 }
 /*This Method Converts 24 hours time format into Minutes
  *Converts Hours into Minutes and Add's it with Minutes to convert toatal time into minutes.
  */
 public int convertTime(String convtime) {
     int var2;
     String[] hrs1 = convtime.split(":");
     int var1 = Integer.parseInt(hrs1[0]); 
     var2 = var1*60 + Integer.parseInt(hrs1[1]);
     return var2;
  }
   /*This Mehhod finds the difference between  double values and returns the absolute value
  * of the result.
  */
    public double  findDiff(double sec , double lat) {
  return Math.abs(sec-lat);
 }
 public String[] drawCircle(double lt1,double  ln1,double radius) {
 
 String[] Cpoints = new String[33];
 double Cy,Cx;
 
      double d2r = Math.PI/180;
      double r2d = 180/Math.PI;
      double Clat = radius * 0.014483;  // Convert statute miles into degrees latitude radius * 0.014483;
     
      double Clng = Clat/Math.cos(lt1*d2r); 
      for (int i=0; i < 33; i++) { 
       
       double d1 = ((i*10000)/16);
    d1=d1/10000;
        double theta = Math.PI * (d1); 
        Cy = lt1 + (Clat * Math.sin(theta)); 
        Cx = ln1 + (Clng * Math.cos(theta)); 
        String P = Cx+","+Cy; 
        Cpoints[i]=P; 
      
      }
return Cpoints;
     
     }
 public String[] render (double lt1,double  ln1,double radius){
   String[] points = new String[73];
   int c=0;
     double distance = ((radius*5280)/3.2808399)/1000;
     for (int i = 0; i < 72; i++) {
       points[c]=destination(lt1,ln1, i * 360/72, distance);
       c++;
       //System.out.println(points[i].toString());
     }
     points[c]=destination(lt1,ln1, 0, distance);
  return points;
     //this._points.push(destination(this._centerHandlePosition, 0, distance) );
     //this._polyline = new GPolyline(this._points, this._color, 6);
 }
 public String destination(double lt2,double ln2, double hdng, double dist) {
  String rest=null;
   int R = 6371; // earth's mean radius in km
   double oX=0.0, oY=0.0,x=0.0,y=0.0;
   double d = dist/R;  // d = angular distance covered on earth's surface
   hdng = hdng * Math.PI / 180; // degrees to radians
   oX = lt2 * Math.PI / 180;
   oY = ln2 * Math.PI / 180;
 
   y = Math.asin( Math.sin(oY)*Math.cos(d) + Math.cos(oY)*Math.sin(d)*Math.cos(hdng) );
   x = oX + Math.atan2(Math.sin(hdng)*Math.sin(d)*Math.cos(oY), Math.cos(d)-Math.sin(oY)*Math.sin(y));
 
   y = y * 180 / Math.PI;
   x = x * 180 / Math.PI;
   rest= x+","+y;
   return rest;
 }
}
 